import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import player.Listener;
public class JoinGameFrame extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtPort;
	private JTextField txtIP;
	private JTextField txtNick;
	private JLabel lblNick;
	private JButton btnAction;

	public JoinGameFrame()
	{
		setBounds(100, 100, 440, 302);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNrPortu = new JLabel("Port Number");
		lblNrPortu.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNrPortu.setBounds(58, 45, 78, 20);
		contentPane.add(lblNrPortu);
		
		txtPort = new JTextField();
		txtPort.setText("4444");
		txtPort.setBounds(146, 45, 190, 20);
		contentPane.add(txtPort);
		txtPort.setColumns(4);
		
		JLabel lblIP = new JLabel("IP");
		lblIP.setHorizontalAlignment(SwingConstants.RIGHT);
		lblIP.setBounds(58, 90, 78, 20);
		contentPane.add(lblIP);
			
		txtIP = new JTextField("localhost");
		txtIP.setBounds(146, 90, 190, 20);
		// ile cyfr ma IP?
		txtIP.setColumns(10); 
		contentPane.add(txtIP);
		
		lblNick = new JLabel("Your Nick");
		lblNick.setBounds(58, 150, 78, 20);
		lblNick.setHorizontalAlignment(SwingConstants.RIGHT);
		contentPane.add(lblNick);
		
		txtNick = new JTextField(15);
		txtNick.setBounds(146, 150, 190, 20);
		contentPane.add(txtNick);
		txtNick.setColumns(10);
		
		btnAction = new JButton("Join");
		btnAction.addActionListener(this);
		
		btnAction.setBounds(146, 193, 190, 23);
		contentPane.add(btnAction);
	}
	public void actionPerformed(ActionEvent event)
	{
		try{
			int port = Integer.parseInt(txtPort.getText());
			String name = txtNick.getText();
			while(name.length()<15){
				name = name + " ";
			}
			if (name.length()>15) name = name.substring(0,15);
			Listener t = new Listener(port,txtIP.getText(),name);
			t.start();
			this.dispose();
		}
		catch( NumberFormatException e){
			String error = e.getMessage().substring(16);
			JOptionPane.showMessageDialog(null, "Incorrect data" + error,
					"Error", JOptionPane.ERROR_MESSAGE);
		}
	}	
}
